package grammatica;

import java.util.ArrayList;

public class Definition {
	ArrayList<Term> terms;
	
	public Definition(){
		terms = new ArrayList<Term>();
	}

	public void addTerm(Term term) {
		terms.add(term);
		
	}

	public String print() {
		String s = "";
		
		for(Term t : terms){
			if(!s.equals(""))s += " ";
			s += t.print();
		}
		
		return s;
	}

	public boolean checkNonTerminals(ArrayList<NonTerminal> definedNonTerminals) {
		boolean mismatches = false;
		for(Term t : terms){
			if(t instanceof NonTerminal){
				mismatches = checkList((NonTerminal)t, definedNonTerminals) || mismatches;				
			}
		}
		return mismatches;
	}

	private boolean checkList(NonTerminal t, ArrayList<NonTerminal> definedNonTerminals) {
		for(NonTerminal nt : definedNonTerminals){
			if(nt.equals(t))return false;
		}
		System.out.println("Non-terminal " + t.getName() + " does not match any defined non-terminal");
		return true;
	}
	
	public boolean isAnEmpty(){
		return terms.get(0) instanceof Empty;
	}

	public Term get(int i) {
		Term t = null;
		try{
			t = terms.get(i);
		}
		catch(IndexOutOfBoundsException e){
			t =  null;
		}
		return t;
	}
	
	public int length(){
		return terms.size();
	}

	public boolean contains(Term t) {
		return terms.contains(t);
	}

	public Term getNext(NonTerminal findId) {
		int i = terms.indexOf(findId);
		i++;
		if(terms.size() <= i)return null;
		else return terms.get(i);
	}

	
}
